package com.HardWare;
import com.friendlyarm.AndroidSDK.HardwareControler;

/**
 * Created by Ziji on 2017/7/10.
 */

public class PCF8591 {
    public static int ADC_Init(int devFD) {
        if (HardwareControler.setI2CSlave(devFD, (byte) (0x48)) < 0)//设置I2c通信硬件地址
        {
            return -1;
        }
        if (HardwareControler.I2CWriteByte(devFD, (byte) 0x00, 0) < 0) {
            return -1;
        } else return 0;
    }

    public static int set_ADC_Channel(int devFD,int channel)
    {
        HardwareControler.I2CWriteByte(devFD,chooseChannel(channel),0);
        return channel;
    }
    public static int readADC0(int devFD)
    {
        HardwareControler.I2CWriteByte(devFD,(byte)0x00,500);
        return HardwareControler.I2CReadByte(devFD,500);
    }
    public static int readADC1(int devFD)
    {
        if(HardwareControler.I2CWriteByte(devFD,(byte)0x01,500)<0)
        {
            return -1;
        }
        return HardwareControler.I2CReadByte(devFD,500);
    }
    public static int[] readADC_by4channel(int devFD)
    {
        HardwareControler.I2CWriteByte(devFD,(byte)0x04,0);
        int[] channel;
        channel=new int[4];
        channel[0]= HardwareControler.I2CReadByte(devFD,100);
        channel[1] = HardwareControler.I2CReadByte(devFD,100);
        channel[2]= HardwareControler.I2CReadByte(devFD,100);
        channel[3]= HardwareControler.I2CReadByte(devFD,100);
        channel[0]= HardwareControler.I2CReadByte(devFD,100);
        channel[1] = HardwareControler.I2CReadByte(devFD,100);
        channel[2]= HardwareControler.I2CReadByte(devFD,100);
        channel[3]= HardwareControler.I2CReadByte(devFD,100);
        return channel;
    }
    public static int readADC_bychannel(int devFD)
    {
        int channel;
        channel= HardwareControler.I2CReadByte(devFD,0);
        channel= HardwareControler.I2CReadByte(devFD,0);
        channel= HardwareControler.I2CReadByte(devFD,0);
        return channel;
    }
    public static int readADC_bychannel(int devFD,int channel)
    {
        switch (channel)
        {
            case 0:
            {
                if(HardwareControler.I2CWriteByte(devFD,(byte)0x00,500)<0)
                {
                    return -1;
                }
                HardwareControler.I2CReadByte(devFD,500);
                HardwareControler.I2CReadByte(devFD,500);
                return HardwareControler.I2CReadByte(devFD,500);
            }
            case 1:
            {
                if (HardwareControler.I2CWriteByte(devFD, (byte) 0x01, 500) < 0) {
                    return -1;
                }
                HardwareControler.I2CReadByte(devFD,500);
                HardwareControler.I2CReadByte(devFD,500);
                return HardwareControler.I2CReadByte(devFD, 500);
            }
            case 2:
            {
                if (HardwareControler.I2CWriteByte(devFD, (byte) 0x02, 500) < 0) {
                    return -1;
                }
                HardwareControler.I2CReadByte(devFD,500);
                HardwareControler.I2CReadByte(devFD,500);
                return HardwareControler.I2CReadByte(devFD, 500);
            }
            case 3:
            {
                if (HardwareControler.I2CWriteByte(devFD, (byte) 0x03, 500) < 0) {
                    return -1;
                }
                HardwareControler.I2CReadByte(devFD,500);
                HardwareControler.I2CReadByte(devFD,500);
                return HardwareControler.I2CReadByte(devFD, 500);
            }
            default:return -1;
        }
    }
    public static byte chooseChannel(int channel)
    {
        switch (channel)
        {
            case 0:
                return 0x00;
            case 1:
                return 0x01;
            case 2:
                return 0x02;
            case 3:
                return 0x03;
            default:
                return 0x00;
        }
    }
}
